<?php

    class Controller_index{
        public function indexAction(){


            $cid = $selectedCate = Tiri_Request::getInstance()->param('cid');
            
            if($cid == 0){
                $curCate = 2;
            }else{
                $curCate = $cid;
            }
            $allLeafCate = AUtil::getChildCate($curCate);

            $orderBy = Tiri_Request::getInstance()->param('order');
            if($orderBy == '' || $orderBy =='both_trial'){
                $orderBy = 'service_data.free_trial + service_data.pay_trial';
            }elseif($orderBy == 'free_trial'){
                $orderBy = ' service_data.free_trial + 0';
            }elseif($orderBy == 'pay_trial'){
                $orderBy = ' service_data.pay_trial + 0';
            }
            //$allLeafCate = AUtil::getChildCate($curCate);


            /** 获取左边的菜单树结构  */
            $parentStack = array();
            $tree = array();
            $parentStack[] = $curCate;

            /** 获取当前cateid  的tree 路径    */
            while(true){
                $c = new Model_Category();
                $c -> loadById($curCate);
                $parentStack [] = $c -> parent_id ;
                /** id=1 为‘全部服务’    */
                if($curCate == 1 || $c -> parent_id == 1){
                    break;
                }
                $curCate = $c-> parent_id;
            }
            $cateTree = $parentStack;

            $c -> setEmpty();
            $pointer = &$tree;
            while(count($parentStack) != 0){
                $curCate = array_pop($parentStack);
                $pointer = &$pointer[$curCate]['sub_tree'];
                $c -> parent_id = $curCate;
                $cSet = $c -> getAllMatch('order by id asc');
                while(!$cSet -> isEnd()){
                    $item = $cSet -> getNext();
                    $pointer[$item -> id ]['cate_name'] = $item -> category;
                }
            }
            /**     以上为生成左边的树菜单     */



            $cur_page = Widget_Pager::getCurrentPage();
            if($cur_page >= 1)$cur_page -= 1;
            $per_page = 10;

            $startPage = $cur_page * $per_page;

            $timeS = date('Ymd' ,time());


            if($cid == 0 ){
                $sql = "SELECT * FROM service LEFT JOIN service_data ON service_data.sid = service.id WHERE service_data.create_time='$timeS' ORDER BY $orderBy DESC LIMIT $startPage , $per_page";
                 $total_service = AUtil::getCountById(1);

            }else{
                $sql = "SELECT * FROM service LEFT JOIN service_data ON service_data.sid = service.id WHERE service_data.create_time='$timeS' AND service.category IN ( $allLeafCate ) ORDER BY $orderBy DESC LIMIT $startPage , $per_page";
                 $total_service = AUtil::getCountById($curCate);

            }
            //echo $sql;
            $db  = Widget_Db::getInstance();
            $serviceData = $db -> query($sql);
           
            // $sSet = $s -> pager($pageConf);
            $pageNav = Widget_Pager::show(str_replace(array('(',')'),'',$total_service) , $per_page);


            Tiri_Template::getInstance() -> assign('pagenav',$pageNav);
            Tiri_Template::getInstance() -> assign('cid',$cid);
            Tiri_Template::getInstance() -> assign('totalNums',$total_service);
            Tiri_Template::getInstance() -> assign('cateTree',$cateTree);
            Tiri_Template::getInstance() -> assign('serviceData',$serviceData);

            Tiri_Template::getInstance() -> assign('tree',$tree['1']['sub_tree']);
            Tiri_Template::getInstance() -> render('index.php');

        }
    }
?>
